//
//  GDFMomentMainListVC.m
//  GlobalDeliciousFood
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 com.mjykj.www. All rights reserved.
//

#import "GDFMomentMainListVC.h"
#import "GDFHeaderView.h"
#import "GDFMomentTableViewCell.h"
#import "GDFMoentModle.h"
#import "GDFMomentDetiallViewController.h"
@interface GDFMomentMainListVC ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)GDFHeaderView *headView;
@property(nonatomic,strong)NSMutableArray *headArray;
@end

@implementation GDFMomentMainListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.7f];
    
    [self addTableView];
    [self setUpData];
}
//解析数据
- (void)setUpData
{
    NSString *urlString = @"http://www.youyuwei.com/api/foodcamera?oauth_version=1.0&oauth_nonce=74d40ccd-aa82-4f10-b5b3-467d0b174637&oauth_consumer_key=5&device_type=android&screen_width=1080&device_id=8c%3Abe%3Abe%3A71%3Acb%3A1d&ver=3&ywsdk_ver=20140507&sys_ver=4.4.4&ver_code=21&channel_id=baidu&oauth_signature=jldAg4ax4w8T20xcPZyMIYg%2Bcq4%3D&x_auth_mode=client_auth&device_token=AuW2jEg1O9f1alTq8V-SVbpyWh8WsL5ixRbT1Say5rL0&oauth_signature_method=HMAC-SHA1&oauth_token=0_9837387abc33331ab&open_udid=8c%3Abe%3Abe%3A71%3Acb%3A1d&app_ver=2.8&app_code=com.yuwei.android&oauth_timestamp=1452484590&screen_height=1920";
    [GDFRequestManager requestWithUrl:urlString parmDic:nil method:GET1 succeedWithData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *dataDic = dic[@"data"];
        NSArray *listAray = dataDic[@"list"];
        self.headArray = [NSMutableArray array];
        self.dataArray = [NSMutableArray array];
        NSDictionary *oneDic = listAray.lastObject;
        GDFMoentModle *model = [[GDFMoentModle alloc]init];
        [model setValuesForKeysWithDictionary:oneDic];
        
        [_headArray addObject:model];
        NSArray *dataArr = oneDic[@"data"];
        for (NSDictionary *towDic in dataArr) {
            GDFMoentModle *listModel = [[GDFMoentModle alloc]init];
            [listModel setValuesForKeysWithDictionary:towDic];
            [_dataArray addObject:listModel];
        }
        [_tableView reloadData];
        
        _headView.titleLable.text = model.title;
    } failedWithError:^(NSError *connectionError) {
        
    }];

}

- (void)addTableView
{
    _tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 100;
    _headView = [[GDFHeaderView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 60)];
    _tableView.tableHeaderView = _headView;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[GDFMomentTableViewCell class] forCellReuseIdentifier:@"Cell"];
}
//添加tableView的数据方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GDFMomentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    GDFMoentModle *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GDFMomentDetiallViewController *detiallVC = [[GDFMomentDetiallViewController alloc]init];
    GDFMoentModle *Mmodle = _dataArray[indexPath.row];
    detiallVC.model = Mmodle;
    [self.navigationController pushViewController:detiallVC animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
